import React, { useEffect, useState, useRef } from 'react';
import { Button, Space, Tag, Avatar, message, Modal } from 'antd';
import ProList from '@ant-design/pro-list';
import {
  CheckOutlined,
  FileTextOutlined,
  LaptopOutlined,
  ReadOutlined,
  CloseOutlined,
  FilePdfOutlined,
  ExclamationCircleOutlined,
} from '@ant-design/icons';
import { useRequest } from 'umi';
import moment from 'moment';
import type { UploadProps } from 'antd';
import type { ProFormInstance } from '@ant-design/pro-components';

import {
  ModalForm,
  ProForm,
  ProFormDateRangePicker,
  ProFormSelect,
  ProFormText,
  ProCard,
  ProTable,
} from '@ant-design/pro-components';

const { confirm } = Modal;

const TrainingPlan = () => {
  const [tableData, setTableData] = useState([]);

  //导师端——修改学生项目或论文的同意状态
  const updateStuPaperProjState = useRequest(
    (stuRuleId = null, isPass = '已完成') => {
      return {
        url: `http://10.1.40.84:9998/educate/updateStuPaperProjState?stuRuleId=${stuRuleId}&isPass=${isPass}`,
        method: 'GET',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
      };
    },
    {
      manual: true,
      onSuccess: () => {
        getUnderStuPaperProj.run();
      },
    },
  );

  //审核
  const showDeleteConfirm = (record: any) => {
    confirm({
      title: '确定通过审核吗?',
      icon: <ExclamationCircleOutlined />,
      content: '通过审核后将计入阶段成果',
      okText: '通过',
      okType: 'danger',
      cancelText: '拒绝',
      onOk() {
        message.info('审核通过！');
        console.log(record);

        updateStuPaperProjState.run(record.id, '1');
        // updateWeekPlanState.run(record.id, '忽略');
      },
      onCancel() {
        message.error('已拒绝！');
        updateStuPaperProjState.run(record.id, '2');
        console.log('Cancel');
      },
    });
  };

  // 上传材料文件
  const props: UploadProps = {
    name: 'material',
    action: 'http://10.1.40.84:9998/stuTrain/uploadMaterial',
    headers: {
      Authorization: localStorage.getItem('token') || '',
    },
    maxCount: 1,
    onChange(info) {
      if (info.file.status !== 'uploading') {
        console.log(info.file, info.fileList);
      }
      if (info.file.status === 'done') {
        message.success(`${info.file.name} file uploaded successfully`);
      } else if (info.file.status === 'error') {
        message.error(`${info.file.name} file upload failed.`);
      }
    },
  };
  // 导师端——导师获取学生提交项目或论文
  const getUnderStuPaperProj = useRequest(
    () => {
      return {
        url: `http://10.1.40.84:9998/educate/getUnderStuPaperProj`,
        method: 'GET',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
      };
    },
    // {
    //     manual: true,
    // },
  );

  const columns = [
    {
      title: '名称',
      dataIndex: 'stuRuleName',
      key: 'stuRuleName',
    },
    {
      title: '类型',
      dataIndex: 'ruleType',
      key: 'ruleType',
    },
    {
      title: '学生名称',
      dataIndex: 'stuName',
      key: 'stuName',
    },

    {
      title: '时间',
      dataIndex: 'createTime',
      key: 'createTime',
      render: (text: any) => <span>{moment(text).format('YYYY-MM-DD')}</span>,
    },
    {
      title: '是否审核',
      dataIndex: 'isPass',
      key: 'isPass',
      render: (text: any) => {
        if (text === '1') {
          return <Tag color={'processing'}>已审核</Tag>;
        } else if (text === '0') {
          return <Tag color={'warning'}>未审核</Tag>;
        } else {
          return <Tag color={'error'}>已拒绝</Tag>;
        }
      },
    },
    {
      title: '操作',
      key: '操作',
      render: (text: any, record: any) => (
        <Space>
          <a
            onClick={() => {
              window.open(record.stuRuleContent);
            }}
          >
            查看
          </a>
          <a key="2" onClick={() => showDeleteConfirm(record)}>
            审核
          </a>
        </Space>
      ),
    },
  ];

  return (
    <ProTable
      // title={"需要审核的列表"}
      headerTitle="需要审核的列表"
      size="small"
      columns={columns}
      rowKey="id"
      pagination={{
        // showQuickJumper: false,
        pageSize: 20,
        hideOnSinglePage: true,
      }}
      // toolBarRender={false}
      search={false}
      dataSource={getUnderStuPaperProj?.data || []}
    />
  );
};

export default TrainingPlan;
